package com.summer.service;

import com.alibaba.fastjson.JSON;
import com.summer.bean.*;
import com.summer.dao.CommentDao;
import com.summer.dao.CourseDao;
import com.summer.dao.LecturerDao;
import com.summer.dao.StudentDao;
import com.summer.result.PersonalDetailResult;
import com.summer.result.Result;
import com.summer.result.SearchResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Description:
 * User: 35391
 * Package: com.summer.service
 * Date: 2025/3/23
 * Time: 下午8:57
 * Version: 1.0
 */
@WebServlet({"/ajax/detail",
        "/ajax/lecturerDetail",
        "/ajax/query",
        "/ajax/personalDetail",
        "/ajax/getByKey"
})
public class AjaxService extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Result<Object> result = new Result<>();
        String path = request.getServletPath();
        if ("/ajax/detail".equals(path)) {
            result = doDetail(request, response);
        }else if ("/ajax/lecturerDetail".equals(path)){
            result = doLecturerDetail(request, response);
        }else if ("/ajax/query".equals(path)){
            result = doQuery(request, response);
        } else if ("/ajax/personalDetail".equals(path)) {
            result = doPersonalDetail(request, response);
        }else if ("/ajax/getByKey".equals(path)){
            result = doGetByKey(request, response);
        }
        if (result != null) {
            String jsonResult = JSON.toJSONString(result);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(jsonResult);
            out.flush();
            out.close();
        }
    }
    /**
     * 根据关键字搜索,支持搜索课程号、教师号、课程名
     * @param request
     * @param response
     */
    private Result doGetByKey(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        String keywords = request.getParameter("keywords");
        System.out.println("keywords = " + keywords);
        List<Course> coursesByCname = CourseDao.selectByLikeName(keywords);
        List<Course> coursesByCno = CourseDao.selectByLikeCno(keywords);
        List<Lecturer> lecturersByTno = LecturerDao.getLecturerDetailByLikeTno(keywords);
//        System.out.println("coursesByCname" + coursesByCname);
//        System.out.println("coursesByCno" + coursesByCno);
//        System.out.println("lecturersByTno" + lecturersByTno);
//        System.out.println("数据查询完成，跳转页面");
        SearchResult searchResult = new SearchResult(coursesByCname, coursesByCno, lecturersByTno);
        return Result.success(searchResult);
    }

    private Result doPersonalDetail(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        response.setContentType("application/json;charset=UTF-8");
        String username = request.getParameter("username");
        if (username != null) {
            PersonalDetailResult personalDetailResult = new PersonalDetailResult();
            Student student = StudentDao.selectByUsername(username);
            if (student == null) {
                personalDetailResult.setTargetPage("SetPersonalInformation");
                return Result.success(personalDetailResult);
            }
            personalDetailResult.setTargetPage("PersonalDetailPage");
            personalDetailResult.setStudent(student);
            return Result.success(personalDetailResult);
        }else{
            return Result.error("用户名不能为空");
        }
    }

    private Result doQuery(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        String keywords = request.getParameter("keywords");
        List<String> comments = CommentDao.selectLikeComment(keywords);
        return Result.success(comments);
    }


    private Result doLecturerDetail(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String Tno = request.getParameter("Tno");
        List<Lecturer> lecturerDetailByTno = LecturerDao.getLecturerDetailByTno(Tno);
        return Result.success(lecturerDetailByTno);
    }

    private Result doDetail(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        List<String> list = new ArrayList<>();
        response.setContentType("application/json;charset=UTF-8");
        list.add("<button id=\"detail" + id + "     \" onclick=\"detail(this)\">详情</button>");
        list.add("<button id=\"relative" + id + "\" onclick=\"detail(this)\">相关课程</button>");
        return Result.success(id);
    }
}
